package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.NameConstant;
import com.itheima.reggie.common.ServerResultVO;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Setmeal
 * @date 2024/10/23
 * @author 武帅丞
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public ServerResultVO<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        log.info("add setmeal:{}", setmealDto);
        return ServerResultVO.success("successfully added setmeal");
    }

    @GetMapping("/page")
    public ServerResultVO<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, queryWrapper);

        Page<SetmealDto> pageDto = new Page<>();
        BeanUtils.copyProperties(pageInfo, pageDto, "records");
        pageDto.setRecords(pageInfo.getRecords().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            setmealDto.setCategoryName(
                    Optional.ofNullable(categoryService.getById(setmeal.getCategoryId()))
                            .map(Category::getName)
                            .orElse(NameConstant.DEFAULT_CATEGORY_NAME)
            );
            return setmealDto;
        }).collect(Collectors.toList()));

        return ServerResultVO.success(pageDto);
    }

    @DeleteMapping
    public ServerResultVO<String> delete(@RequestParam List<Long> ids) {
        setmealService.deleteWithDish(ids);
        log.info("delete setmeal, ids:{}", ids);
        return ServerResultVO.success("successfully deleted");
    }
}